package com.clkj.xiuzheng.service.shangcheng;

import com.clkj.xiuzheng.dao.shangcheng.ScBrandMapper;
import com.clkj.xiuzheng.dao.shangcheng.ScProductCategoryMapper;
import com.clkj.xiuzheng.dao.shangcheng.ScProductMapper;
import com.clkj.xiuzheng.pojo.common.OSSUtil;
import com.clkj.xiuzheng.pojo.shangcheng.ProductBrand;
import com.clkj.xiuzheng.pojo.shangcheng.ProductCategory;
import com.clkj.xiuzheng.pojo.shangcheng.ProductCategoryRequest;
import com.clkj.xiuzheng.pojo.shangcheng.SProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class ScCategoryServiceImpl implements ScCategoryService{

    @Autowired
    private ScProductCategoryMapper scProductcategoryMapper;
    @Autowired
    private ScProductMapper productMapper;

    @Autowired
    private ScBrandMapper brandMapper;
    private Map map;

    @Override
    public List<ProductCategory> getProductCategory(Integer parentId, Integer companyId) {
        Example example =  new Example(ProductCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId",parentId);
        if (companyId!=null){
            criteria.andEqualTo("companyId",companyId);
        }
        List<ProductCategory> list = scProductcategoryMapper.selectByExample(example);

        return list;
    }

    @Override
    public void addCategory(Integer parentId,String categoryNames,Integer flag,Integer companyId) {
            List<ProductCategory> productCategoryList = new ArrayList<>();
            String[] categoryNameList = categoryNames.split(",");
            for (String categoryName : categoryNameList) {
            ProductCategory productCategory = new ProductCategory();
            productCategory.setCategoryName(categoryName);
            productCategory.setParentId(parentId);
            productCategory.setCategoryStatus(1);
            productCategory.setCategoryLevel(flag);
            productCategory.setCompanyId(companyId);
            productCategoryList.add(productCategory);
        }
         scProductcategoryMapper.insertList(productCategoryList);
    }

    @Override
    public void DownCategory(String categoryIdList) {
        String[] split = categoryIdList.split(",");
        Example example =  new Example(ProductCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("productCategoryId", Arrays.asList(split));
        ProductCategory productCategory = new ProductCategory();
        productCategory.setCategoryStatus(0);
        scProductcategoryMapper.updateByExampleSelective(productCategory,example);
    }

    @Override
    public void updateCategoryName(String categoryName, Integer categoryId) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setProductCategoryId(categoryId);
        productCategory.setCategoryName(categoryName);
        scProductcategoryMapper.updateByPrimaryKeySelective(productCategory);
    }

    @Override
    public void updateCategoryStatus(String ids, Integer status) {
        String[] split = ids.split(",");
        Example example =  new Example(ProductCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("productCategoryId", Arrays.asList(split));
        ProductCategory productCategory = new ProductCategory();
        productCategory.setCategoryStatus(status);
        scProductcategoryMapper.updateByExampleSelective(productCategory,example);
    }

    @Override
    public Map listCategory(Integer firstId, Integer secId,Integer brandId) {
        map = new HashMap<>();
        List<ProductCategory> firstCategories=null;
        Example example = new Example(ProductCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("categoryStatus",1).andEqualTo("categoryLevel",0);
//        if(firstId==0){
            firstCategories = scProductcategoryMapper.selectByExample(example);
//        }
        //一级分类
        map.put("firstCategories",firstCategories);
        criteria = null;

        List<ProductCategory> secCategories= null;
        example = new Example(ProductCategory.class);
        criteria = example.createCriteria();
        if(firstId==0){
            criteria.andEqualTo("parentId",firstCategories.get(0).getProductCategoryId());
            secCategories= scProductcategoryMapper.selectByExample(example);
        }else{
            criteria.andEqualTo("parentId",firstId);
            secCategories= scProductcategoryMapper.selectByExample(example);
        }
        //二级分类
        map.put("secCategories",secCategories);


        //品牌商
        List<ProductBrand> brandList =null;
        if(brandId==0){
            example = new Example(ProductBrand.class);
            criteria = example.createCriteria();
            criteria.andEqualTo("brandStatus",1);
            brandList =brandMapper.selectByExample(example);
        }

        map.put("brandList",brandList);

        example = new Example(SProduct.class);
        criteria = example.createCriteria();
        criteria.andEqualTo("productStatus",1);
        if(secId==0){
            criteria.andEqualTo("categoryTwoId",secCategories.get(0).getProductCategoryId());
        }else{
            criteria.andEqualTo("categoryTwoId",secId);
        }
        if(brandId!=0){
            criteria.andEqualTo("productBrandId",brandId);
        }
        example.orderBy("productSaleType").asc();

        List<SProduct> products = productMapper.selectByExample(example);




        map.put("products", products);
        map.put("msg", "firstCategories：所有的一级分类；secCategories：该一级分类下面的二级分类；products：该二级分类下面的商品");


        return map;
    }

    @Override
    public void addFirstCategory(ProductCategoryRequest categories, HttpServletRequest request) {
        List<ProductCategory> categoryList = categories.getCategoryList();

        List<MultipartFile> files = ((MultipartRequest) request).getFiles("file");

        List<ProductCategory> newList = new ArrayList<>();

        for (int i = 0;i <categoryList.size();i++) {
            String img = OSSUtil.uploadingImg(files.get(i), "xiuzheng/scCategory");
            categoryList.get(i).setCategoryImg(img);
            newList.add(categoryList.get(i));
        }
        if(newList.size()>0){
            scProductcategoryMapper.insertList(newList);
        }
        categoryList = null;
        files=null;
        newList= null;
    }
}
